package edu.nanyang.controller;

import edu.nanyang.domain.ClassInfo;
import edu.nanyang.service.ClassInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin/class")
public class ClassInfoController extends BaseController{
    @Resource
    private ClassInfoService classInfoService;
    @GetMapping("/delete")
    public String delete(int id){
        int result=classInfoService.delete(id);
        if(result>0){
            request.setAttribute("tip_info","删除班级信息成功！");
        }else{
            request.setAttribute("tip_info","删除班级信息失败！");
        }
        return "forward:findall";
    }
    @GetMapping("/findall")
    public String findAll(){
        List<ClassInfo> classInfoList=classInfoService.findAll();
        request.setAttribute("class_info",classInfoList);
        return "admin/class/class_list";
    }

    @GetMapping("/addpage")
    public String addPage(){
        return "admin/class/class_add";
    }

    @PostMapping("/addsave")
    public String addSave(ClassInfo classInfo){
        int result=classInfoService.add(classInfo);
        if(result>0){
            request.setAttribute("tip_info","添加班级信息成功！");
        }else{
            request.setAttribute("tip_info","添加班级信息失败！");
        }
        return "result";
    }

    @GetMapping("/updatepage")
    public String updatePage(int id){
        ClassInfo classInfo=classInfoService.getById(id);
        request.setAttribute("class_info",classInfo);
        return "admin/class/class_edit";
    }
    @PostMapping("/updatesave")
    public String updateSave(ClassInfo classInfo){
        int result=classInfoService.update(classInfo);
        if(result>0){
            request.setAttribute("tip_info","修改班级信息成功！");
        }else{
            request.setAttribute("tip_info","修改班级信息失败！");
        }
        return "result";
    }



}
